package com.exigen.perflabstat;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

import com.exigen.perflabstat.models.UploadedFile;
import com.exigen.perflabstat.services.Template2Service;


@ManagedBean
@SessionScoped
public class FileUpload {
	
	private Template2Service t2s;
	private List<UploadedFile> uploadedFiles = new ArrayList<UploadedFile>();
	private boolean render;

	
	//Adds a file to the list
	public void addFile(FileUploadEvent event) {
		UploadedFile file = new UploadedFile(t2s.loadFile(event.getFile()), "selected");
		if(!uploadedFiles.contains(file)) {
			uploadedFiles.add(file);
			file.getFile().deleteOnExit();
		}
		else
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File already in the list"));
	}
	
	
	//Handles file upload for every file
    public void handleFileUpload(File file) {
    		if(file == null) {
    			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error, could not load file!")); 
    			return;
    		}
    	
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(file.getName() + " was successfully uploaded!")); 
    		
    		//Get/parse filename data
    		t2s.loadFilenameData(file.getName());	
	        
    		if(t2s.getTemplateNumber() == 1)
    			t2s.parseFileTemplate1(file);
    		if(t2s.getTemplateNumber() == 2)
	        	t2s.parseFileTemplate2(file);
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(file.getName() + " was successfully parsed!")); 
    }
    
    
    //Deletes selected uploaded file from list
    public void deleteFile(UploadedFile file) {
    	if(uploadedFiles.contains(file)) {
    		file.getFile().delete();
    		uploadedFiles.remove(file);
    	}
    	else
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File already deleted")); 
    }
    
    
    //Removes uploaded file panel and all its contents
    public void close() {
    	if(uploadedFiles.isEmpty())
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("List already empty")); 
    	else {
    		for(UploadedFile file: uploadedFiles)
    			file.getFile().delete();
    		uploadedFiles.clear();
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Files removed")); 
    	}
    }
    
    
    //Starts the parsing process of uploaded files
    public void start() {
    	for(UploadedFile file: uploadedFiles) {
    		if(file.getStatus() == "loaded")
    			continue;
    		
    		file.setStatus("processing");
    		handleFileUpload(file.getFile());
    		file.setStatus("loaded");
    	}
    }
    
    
    //Getters and Setters -----------------------------------------------


	public Template2Service getT2s() {
		return t2s;
	}

	public void setT2s(Template2Service t2s) {
		this.t2s = t2s;
	}

	public List<UploadedFile> getUploadedFiles() {
		return uploadedFiles;
	}

	public void setUploadedFiles(List<UploadedFile> uploadedFiles) {
		this.uploadedFiles = uploadedFiles;
	}

	//Checks if it is needed to render the uploaded file panel
	public boolean isRender() {
		if(uploadedFiles.isEmpty())
    		this.render = false;
    	else
    		this.render = true;
		
		return this.render;
	}

	public void setRender(boolean render) {
		this.render = render;
	}

}